//
//  LMEditingLocalUserViewController.m
//  LM
//
//  Created by Kien Tran on 2/2/13.
//  Copyright (c) 2013 Kien Tran. All rights reserved.
//

#import "LMEditingLocalUserViewController.h"
#import "LMLocalUser.h"
#import "LMConstant.h"
#import "LMBirthdaySelectionViewController.h"
#import "LMSexSelectionViewController.h"
#import "ActionSheetDatePicker.h"


@interface LMEditingLocalUserViewController ()

@end

@implementation LMEditingLocalUserViewController
@synthesize localUser = _localUser, nameTextField = _nameTextField, sexLabel = _sexLabel, birthdayLabel = _birthdayLabel;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (!self.nameTextField.text || [self.nameTextField.text length] == 0) {
        self.nameTextField.text = _localUser.userName;
    }
        
    self.sexLabel.text = _localUser.sex ? @"Male" : @"Female";
    
    if (_localUser.birthday && ([self.birthdayLabel.text length] == 0)) {
        self.birthdayLabel.text = [_localUser getBirthdayString];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    if (indexPath.row == 0) {
        [self.nameTextField becomeFirstResponder];
    } else if (indexPath.row == 2) {
        NSDate *date = _localUser.birthday ? _localUser.birthday : [NSDate date];
       ActionSheetDatePicker *actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:date target:self action:@selector(dateWasSelected:element:) origin:tableView];
        actionSheetPicker.hideCancel = YES;
        [actionSheetPicker showActionSheetPicker];
    }
}

- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element {
    _localUser.birthday = selectedDate;
    
    //may have originated from textField or barButtonItem, use an IBOutlet instead of element
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = kDATE_FORMAT;
    
    self.birthdayLabel.text = [dateFormatter stringFromDate:_localUser.birthday];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:kPushToSexSelectionViewController]) {
        LMSexSelectionViewController *sexController = [segue destinationViewController];
        sexController.localUser = _localUser;
    } else if ([[segue identifier] isEqualToString:kPushToBirthdaySelectionViewController]) {
        LMBirthdaySelectionViewController *birthdayCOntroller = [segue destinationViewController];
        birthdayCOntroller.localUser = _localUser;
    }
}
- (void)viewDidUnload {
    [self setNameTextField:nil];
    [self setSexLabel:nil];
    [self setBirthdayLabel:nil];
    [super viewDidUnload];
}
- (IBAction)save:(id)sender {
    _localUser.userName = self.nameTextField.text;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = kDATE_FORMAT;
    
    _localUser.birthday = [dateFormatter dateFromString:self.birthdayLabel.text];
    
    [LMLocalUser setSharedLocalUser:_localUser];
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}
@end
